$().ready(function() {
	validateRule();

	$("#types").val( sessionStorage.getItem('types') )

	$.ajax({
		cache : true,
		type : "get",
		url : "/jzApi/examinations/selExaminationCom",
		data:{
		},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {

			if (data.code == 0) {

				$('#cbatchId').selectPage({
					showField: 'name',
					keyField: 'id',
					data: data.data,
					eAjaxSuccess : function(d){
						var result;
						if(d) result = d.data;
						else result = undefined;
						return result;
					},
					eSelect : function(data){
						$('#cbatchId').val(data.id);
					}
				});

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});

	var nations = ["汉族","蒙古族","回族","藏族","维吾尔族","苗族","彝族","壮族","布依族","朝鲜族","满族","侗族","瑶族","白族","土家族",
		"哈尼族","哈萨克族","傣族","黎族","傈僳族","佤族","畲族","高山族","拉祜族","水族","东乡族","纳西族","景颇族","柯尔克孜族",
		"土族","达斡尔族","仫佬族","羌族","布朗族","撒拉族","毛南族","仡佬族","锡伯族","阿昌族","普米族","塔吉克族","怒族", "乌孜别克族",
		"俄罗斯族","鄂温克族","德昂族","保安族","裕固族","京族","塔塔尔族","独龙族","鄂伦春族","赫哲族","门巴族","珞巴族","基诺族"];
	var nation = $("#nation");
	var nation2 = $("#nation2");
	for ( var i=0;i<nations.length;i++) {
		var a=nations[i];
		nation.append("<option value='"+nations[i]+"'>"+a+"</option>");
		nation2.append("<option value='"+nations[i]+"'>"+a+"</option>");
	}

    //省
	$.ajax({
		cache : true,
		type : "get",
		url : "/jzweb/provincial/listProvincial",
		data:{
			pageNumber:1,
			pageSize:100
		},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {

			if (data.code == 0) {

				$('#province').selectPage({
					showField: 'provincial',
					keyField: 'pid',
					data: data.data.records,
					eAjaxSuccess : function(d){

					},
					eSelect: function (data) {
						if(data){
							$.ajax({
								cache : true,
								type : "get",
								url : "/jzweb/provincial/listCity",
								data : {
									pid:data.pid,
									pageNumber:1,
									pageSize:100
								},// 你的formid
								async : false,
								error : function(request) {
									parent.layer.alert("Connection error");
								},
								success : function(datas) {
									if (datas.code == 0) {
										var arr = new Array();
										$.each(datas.data.records, function(i,row){
											if(row.pid == data.pid) arr.push(row);

											console.log('data',data.pid)
											console.log('row',row.pid)
											console.log('type',row.pid==data.pid)
										});
										//从城市数据中过滤省份是选择的省，并刷新城市的选择器数据
										if(arr.length){
											console.log('arr',arr)
											$('#city').selectPageData(arr)
										};
									} else {
										parent.layer.alert(data.msg)
									}

								}
							});
						}
					},
					eClear: function(){//清空时，同时清空城市内容
						$('#city').selectPageClear();
						$('#city').selectPageData([]);
					}
				});

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});
	//城市选择器的初始化，使用空数据进行初始化
	$('#city').selectPage({
		showField: 'city',
		keyField: 'cid',
		data: []
	});

});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function save() {

	$("#types").val(sessionStorage.getItem('types'));

	$('#provinceText').val($('#province').selectPageText())
	$('#cityText').val($('#city').selectPageText())

	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/careSignUpComReality/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			// name : {
			// 	required : true
			// },
			// idNumber : {
			// 	required : true
			// },
			// ticketNumber : {
			// 	required : true
			// },
			cbatchId : {
				required : true
			},
			// competitionType : {
			// 	required : true
			// },
			unitName : {
				required : true
			}
		},
		messages : {
			// name : {
			// 	required : icon + "请输入姓名"
			// },
			// idNumber : {
			// 	required : icon + "请输入身份证号码"
			// },
			// ticketNumber : {
			// 	required : icon + "请输入联系方式"
			// },
			cbatchId : {
				required : icon + "请选择所属竞赛"
			},
			// competitionType : {
			// 	required : icon + "请选择竞赛类型"
			// },
			unitName : {
				required : icon + "请输入参赛单位"
			}
		}
	})
}